package com.yyj.controller;

import com.yyj.mapper.StudentMapper;
import com.yyj.pojo.entity.Student;
import com.yyj.pojo.vo.StudentVo;
import com.yyj.service.StudentService;
import com.yyj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: qy164-springboot4
 * @description:
 * @author: 杨英俊
 * @create: 2023-04-12 20:04
 **/
@RestController

@Api(tags = "学生操作API")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentMapper studentMapper;

    @PostMapping("list/{current}/{pageSize}")
    @ApiOperation(value = "根据条件分页查询学生信息")
    public Result list(
            @ApiParam(value = "当前页面",name = "current",required = true,defaultValue = "1")
            @PathVariable Integer current,
            @ApiParam(value = "每页条数",name = "pageSize",required = true,defaultValue = "5")
            @PathVariable Integer pageSize,
            @RequestBody StudentVo studentVo
            ){
        return studentService.findByPages(current,pageSize,studentVo);
    }

    @DeleteMapping("delete/{sid}")
    @ApiOperation(value ="根据学生id删除" )
    public Result delete(
            @ApiParam(name = "sid",value = "学生编号",required = true)
            @PathVariable Integer sid
            ){
        int i = studentMapper.deleteById(sid);
        return new Result(200,"删除成功",i);
    }

    @PutMapping("insert")
    @ApiOperation(value = "添加数据")
    public Result insert(
            @ApiParam(name = "student" ,value = "学生信息" ,required = true)
            @RequestBody Student student
    ){

        int insert = studentMapper.insert(student);
        return new Result(200,"添加成功",insert);
    }

    @PutMapping("update/{sid}")
    @ApiOperation(value = "修改数据")
    public Result update(
            @ApiParam(name = "student",value = "学生信息",required = true)
            @RequestBody Student student
    ){
        int i = studentMapper.updateById(student);
        return new Result(200,"修改成功",i);
    }

    @GetMapping("selectById/{sid}")
    @ApiOperation(value = "根据学生编号进行查询")
    public Result selectById(
            @ApiParam(name = "sid" ,value = "学生编号",required = true)
            @PathVariable Integer sid
    ){
        Student student = studentMapper.selectById(sid);
        return new Result(200,"查询成功",student);
    }

}
